package com.up.management.service.impl;

import com.up.common.utils.DateUtils;
import com.up.management.domain.dto.EventCommentDTO;
import com.up.management.domain.entity.EventComment;
import com.up.management.domain.vo.EventCommentVO;
import com.up.management.mapper.EventCommentMapper;
import com.up.management.service.IEventCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论管理Service业务层处理
 * 
 * @author cloud
 * @date 2025-10-13
 */
@Service
public class EventCommentServiceImpl implements IEventCommentService
{
    @Autowired
    private EventCommentMapper eventCommentMapper;

    @Autowired
    private com.up.management.mapper.EventMapper eventMapper;

    @Autowired
    private com.up.management.mapper.UserMapper userMapper;

    /**
     * 查询评论管理
     * 
     * @param commentId 评论管理主键
     * @return 评论管理
     */
    @Override
    public EventCommentVO selectEventCommentByCommentId(Long commentId)
    {
        EventComment entity = eventCommentMapper.selectEventCommentByCommentId(commentId);
        return com.up.management.domain.vo.EventCommentVO.fromEntity(entity);
    }

    /**
     * 查询评论管理列表
     * 
     * @param eventComment 评论管理
     * @return 评论管理
     */
    @Override
    public List<EventCommentVO> selectEventCommentList(EventCommentDTO eventComment)
    {
        EventComment query = eventComment == null ? new EventComment() : eventComment.toEntity();
        List<EventComment> list = eventCommentMapper.selectEventCommentList(query);
        List<EventCommentVO> vos = list.stream().map(com.up.management.domain.vo.EventCommentVO::fromEntity).collect(Collectors.toList());
        // 填充活动名称与用户名
        for (int i = 0; i < list.size(); i++) {
            EventComment ec = list.get(i);
            EventCommentVO vo = vos.get(i);
            // 活动名称
            if (ec.getEventId() != null) {
                com.up.management.domain.entity.Event ev = eventMapper.selectEventById(ec.getEventId());
                if (ev != null) { vo.setEventName(ev.getName()); }
            }
            // 用户名
            if (ec.getUserId() != null) {
                com.up.management.domain.entity.User u = userMapper.selectUserById(ec.getUserId());
                if (u != null) { vo.setUsername(u.getUsername()); }
            }
            // 父评论内容（若数据库已有则保留，否则按ID查询补充）
            if ((vo.getParentContent() == null || vo.getParentContent().isEmpty()) && ec.getParentId() != null) {
                com.up.management.domain.entity.EventComment parent = eventCommentMapper.selectEventCommentByCommentId(ec.getParentId());
                if (parent != null) { vo.setParentContent(parent.getContent()); }
            }
            // 顶级评论内容（根据 topId 查询）
            if (ec.getTopId() != null) {
                com.up.management.domain.entity.EventComment top = eventCommentMapper.selectEventCommentByCommentId(ec.getTopId());
                if (top != null) { vo.setTopContent(top.getContent()); }
            }
        }
        return vos;
    }

    /**
     * 新增评论管理
     * 
     * @param eventComment 评论管理
     * @return 结果
     */
    @Override
    public int insertEventComment(EventCommentDTO eventComment)
    {
        EventComment entity = eventComment.toEntity();
        entity.setCreateTime(DateUtils.getNowDate());
        return eventCommentMapper.insertEventComment(entity);
    }

    /**
     * 修改评论管理
     * 
     * @param eventComment 评论管理
     * @return 结果
     */
    @Override
    public int updateEventComment(EventCommentDTO eventComment)
    {
        EventComment entity = eventComment.toEntity();
        entity.setUpdateTime(DateUtils.getNowDate());
        return eventCommentMapper.updateEventComment(entity);
    }

    /**
     * 批量删除评论管理
     * 
     * @param commentIds 需要删除的评论管理主键
     * @return 结果
     */
    @Override
    public int deleteEventCommentByCommentIds(Long[] commentIds)
    {
        return eventCommentMapper.deleteEventCommentByCommentIds(commentIds);
    }

    /**
     * 删除评论管理信息
     * 
     * @param commentId 评论管理主键
     * @return 结果
     */
    @Override
    public int deleteEventCommentByCommentId(Long commentId)
    {
        return eventCommentMapper.deleteEventCommentByCommentId(commentId);
    }
}
